package com.example.expresslove;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TheEndActivity extends Activity implements OnClickListener {

	private Button yes;
	private Button no;
	private TextView text_line1, text_line2;
	private int eCount = 0;//用于拒绝判断2第三次退出APP
	String phoneNumber = "0123456789";//电话号码在config里面修改
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.end);
		initView();
		initEvent();
	}

	private void initEvent() {
		yes.setOnClickListener(this);
		no.setOnClickListener(this);
	}

	private void initView() {
		yes = (Button) findViewById(R.id.id_yes);
		no = (Button) findViewById(R.id.id_no);
		text_line1 = (TextView) findViewById(R.id.textView1);
		text_line2 = (TextView) findViewById(R.id.textView2);
		Typeface face = Typeface.createFromAsset(getAssets(),"fonts/english.ttf");
		text_line1.setTypeface(face);
		text_line2.setTypeface(face);
		yes.setTypeface(face);
		no.setTypeface(face);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.id_yes:
				if(checkPermission()){
					callMe();
				}
				break;
			case R.id.id_no:
				alertHer();
				break;
			default:
				break;
		}
	}

	private void callMe() {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
				+ phoneNumber));
		startActivity(intent);
		//finish();
	}

	private boolean checkPermission(){
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
				// TODO: Consider calling
				//    Activity#requestPermissions
				// here to request the missing permissions, and then overriding
				//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
				//                                          int[] grantResults)
				// to handle the case where the user grants the permission. See the documentation
				// for Activity#requestPermissions for more details.
				requestPermissions(new String[]{Manifest.permission.CALL_PHONE},1);
				return false;
			}
			else{
				return true;
			}
		}else {
			return true;
		}
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if (grantResults.length > 0) {
			switch (requestCode) {
				case 1:
					if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
						callMe();
					}
					break;
				default:
					break;
			}
		}

	}

	private void alertHer() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				TheEndActivity.this);
		builder.setIcon(R.drawable.wenhao);
		builder.setTitle("回电话给我吧，亲爱的");
		builder.setPositiveButton("可怜他，回吧", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				//检查权限并授予，有权限则打电话
				if(checkPermission()){
					callMe();
				}
				//finish();
			}
		});
		builder.setNegativeButton("打死也不回", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				if(eCount<2){//三次拒绝就退出应用
					Toast.makeText(getApplicationContext(),"亲爱的别这么狠心嘛！",Toast.LENGTH_SHORT).show();
					eCount++;
				}else {
					finish();
				}
			}
		});
		builder.create().show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		MainActivity.MP3.stop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			return false;
		}//锁定home太狠了，不宜使用
		/*else if(keyCode == KeyEvent.KEYCODE_HOME){
			return false;
		}*/
		return false;
	}
}
